package com.maikang.exammall.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import com.maikang.exammall.serializer.ZonedDateTimeDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class JacksonConfiguration {
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());

//        ObjectMapper build = Jackson2ObjectMapperBuilder.json().featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
//                .modules(new SimpleModule() {
//                    {
//                /*
//                    注册对于String类型值对象的反序列化器
//                    对于反序列化器直接new StdDeserializer的子类StdScalarDeserializer完成
//                */
//                        addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) {
//                            @Override
//                            public String deserialize(JsonParser jp, DeserializationContext context) throws IOException {
//                                return StringUtils.trimWhitespace(jp.getValueAsString());
//                            }
//                        });
//                        // ... 也可自定义其他类型序列化和反序列化器，例如：蛋疼的日期类型...
//                    }
//                },
//                        javaTimeModule).build(); // ISODate
        StringTrimModule stringTrimModule = new StringTrimModule();

        ObjectMapper build = Jackson2ObjectMapperBuilder.json().featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .modules(stringTrimModule,
                        javaTimeModule).build(); // ISODate

//        build.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return build;
    }
}
